<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="检索信息" prop="search">
        <el-input v-model="queryParams.search" placeholder="请输入检索信息" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="地址" prop="code">
        <el-cascader @change="handleChange" v-model="queryParams.code" :options="addressList" :props="props" collapse-tags
          clearable>
        </el-cascader>
      </el-form-item>

      <el-form-item label="状态" prop="actStatus">
        <el-select v-model="queryParams.actStatus" placeholder="选择状态">
          <el-option label="正常" value="0"></el-option>
          <el-option label="下架" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间" prop="actBtime">
        <el-date-picker clearable v-model="queryParams.actBtime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="actEtime">
        <el-date-picker clearable v-model="queryParams.actEtime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['manage:activity:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['manage:activity:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['manage:activity:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="activityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动名称" align="center" prop="actTitle" />
      <el-table-column label="活动简介" align="center" prop="actIntro" />
      <el-table-column label="封面" prop="actPhoto" width="110" height="50">
        <template slot-scope="scope">
          <el-image :src="scope.row.actPhoto" fit="cover" style=" width:20; height:20;">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>

      <el-table-column label="负责人" align="center" prop="actCharge" />
      <el-table-column label="开始时间" align="center" prop="actBtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.actBtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="actEtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.actEtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="actStatus">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.actStatus" active-value="0" inactive-value="1"
            @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="活动地点" align="center" prop="actPlaceDetail" width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['manage:activity:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['manage:activity:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改活动记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活动名称" prop="actTitle">
          <el-input v-model="form.actTitle" placeholder="请输入活动名称" />
        </el-form-item>
        <el-form-item label="活动简介" prop="actIntro">
          <el-input type="textarea" :rows="2" v-model="form.actIntro" placeholder="请输入活动简介" />
        </el-form-item>
        <el-form-item label="封面" prop="actPhoto">
          <imageUpload :value="form.actPhoto" :limit="1" @input="getImageFileList"></imageUpload>
        </el-form-item>
        <el-form-item label="地址" prop="areaCode">
          <el-cascader ref="cascader" @change="handleChange1" v-model="form.areaCode" :options="addressList"
            :props="props" collapse-tags clearable>
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="actPlace">
          <el-input v-model="form.actPlace" placeholder="请输入具体地点" />
        </el-form-item>
        <el-form-item label="负责人" prop="actCharge">
          <el-input v-model="form.actCharge" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="开始时间" prop="actBtime">
          <el-date-picker clearable v-model="form.actBtime" type="date" value-format="yyyy-MM-dd" placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="actEtime">
          <el-date-picker clearable v-model="form.actEtime" type="date" value-format="yyyy-MM-dd" placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="actRem">
          <el-input type="textarea" :rows="2" v-model="form.actRem" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="活动内容">
          <editor v-model="form.actContent" :min-height="192" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { changeUserStatus, getAddressAndCode, listActivity, getActivity, delActivity, addActivity, updateActivity } from "@/api/manage/activity";

export default {
  name: "Activity",
  data () {
    return {
      // 地址数据
      addressList: [],
      // 分类 级联数据
      props: {
        children: 'children',
        label: 'name',
        value: "code",
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动记录表格数据
      activityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        search: null,
        code: null,
        actBtime: null,
        actEtime: null,
        actStatus: null,
        flag: "backstage"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created () {
    this.getList();
    this.getAddressList();
  },
  methods: {
    getImageFileList (strs) {
      console.log(strs);
      this.form.actPhoto = strs
    },
    handleChange1 (val) {
      this.form.areaCode = val[2]
      let nodesInfo = this.$refs['cascader'].getCheckedNodes()
      console.log(nodesInfo);
      this.form.districtName = nodesInfo[0].label
      this.form.cityName = nodesInfo[0].parent.label
      this.form.provinceName = nodesInfo[0].parent.parent.label
    },
    // 状态修改
    handleStatusChange (row) {
      let text = row.opStatus === "0" ? "上架" : "下架";
      this.$modal.confirm('确认要"' + text + '""' + row.actTitle + '"内容吗？').then(function () {
        return changeUserStatus(row.actId, row.actStatus);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function () {
        row.actStatus = row.actStatus === "0" ? "1" : "0";
      });
    },
    // 获取地址级联菜单
    getAddressList () {
      getAddressAndCode().then(res => {
        this.addressList = res
      })
    },
    handleChange (val) {
      this.queryParams.code = val[2]
    },
    /** 查询活动记录列表 */
    getList () {
      this.loading = true;
      listActivity(this.queryParams).then(response => {
        this.activityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.actId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加活动记录";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const actId = row.actId || this.ids
      getActivity(actId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改活动记录";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addActivity(this.form).then(response => {
            this.$modal.msgSuccess(response.msg);
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const actIds = row.actId || this.ids;
      this.$modal.confirm('是否确认删除活动记录编号为"' + actIds + '"的数据项？').then(function () {
        return delActivity(actIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('manage/activity/export', {
        ...this.queryParams
      }, `activity_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
